package qwq.zyu.TeamColor;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;

public class qwqScheduledTeamColorTask extends BukkitRunnable {
    private boolean isEnabled = false;

//    public qwqScheduledTeamColorTask(boolean isEnabled) {
//        this.isEnabled = isEnabled;
//    }

    public qwqScheduledTeamColorTask(){
        this.isEnabled = false;
    }



    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    @Override
    public void run() {
        if ( !isEnabled ){
//            Bukkit.getServer().broadcastMessage("[qwq_debug]disabled dye");
            return;
        }

        Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
        Objective teamDisplay = scoreboard.getObjective("teamDisplay");
        if (teamDisplay == null) {
//            Bukkit.getServer().broadcastMessage("[qwq_debug]cannot find scoreboard");
            return;
        }

        for (Player player: Bukkit.getOnlinePlayers()){
            Score score = teamDisplay.getScore(player.getName());
            int value = score.isScoreSet() ? score.getScore() : 0;
//            Bukkit.getServer().broadcastMessage("[qwq_debug]playername: " + player.getName());

            switch (value){
                case 1:
                    player.setDisplayName(ChatColor.RED + "[红] " + player.getName() + ChatColor.RESET);
                    player.setPlayerListName(ChatColor.RED + "[红] " + player.getName() + ChatColor.RESET);
                    break;
                case 2:
                    player.setDisplayName(ChatColor.YELLOW + "[黄] " + player.getName() + ChatColor.RESET);
                    player.setPlayerListName(ChatColor.YELLOW + "[黄] " + player.getName() + ChatColor.RESET);
                    break;
                case 3:
                    player.setDisplayName(ChatColor.GREEN + "[绿] " + player.getName() + ChatColor.RESET);
                    player.setPlayerListName(ChatColor.GREEN + "[绿] " + player.getName() + ChatColor.RESET);
                    break;
                case 4:
                    player.setDisplayName(ChatColor.BLUE + "[蓝] " + player.getName() + ChatColor.RESET);
                    player.setPlayerListName(ChatColor.BLUE + "[蓝] " + player.getName() + ChatColor.RESET);
                    break;
                default:
                    player.setDisplayName(ChatColor.WHITE + player.getName() + ChatColor.RESET);
                    player.setPlayerListName(ChatColor.WHITE + player.getName() + ChatColor.RESET);
                    break;
            }
        }
    }
}
